home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6181 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.os.msdos.programmer,comp.lang.c
  4. Subject: Re: open vs fopen?
  5. Date: Thu, 22 Feb 96 23:28:36 GMT
  6. Organization: none
  7. Message-ID: <825031716snz@genesis.demon.co.uk>
  8. References: <uEYFxc9nX8WX083yn@mbnet.mb.ca> <4f8qbg$549@chleuasme.francenet.fr> <4gg5nk$he1@salyko.cube.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4gg5nk$he1@salyko.cube.net>
  15.            medardus@cube.net "Andreas Karnetzki" writes:
  16.  
  17. >sherlock@micronet.fr schrieb:
  18. >> natewild@mbnet.mb.ca (Nathan T. Wild) wrote:
  19. >
  20. >> >In C, why would one use open and DOS int handles rather than fopen and
  21. >> >stdio file handles?
  22. >
  23. >> >Is there some speed advantage or differnet functionality?
  24. >
  25. >
  26. >> because files opened using open and DOS files handles can be read and
  27. >> write at the same time, while stdio files can't.
  28. >
  29. >That's not correct, I think. You can open files for read/write with the
  30. >the stdio routines, too. Just use the "w+", "r+" or "a+" open modes with
  31. >fopen().
  32. >
  33. >        fopen( "New.dat", "w+" );
  34. >
  35. >opens a file starting with 0 bytes for writing but with the possibility 
  36. >to switch to read mode without the need of closing it.
  37. >
  38. >        fopen( "Old.dat", "r+" );
  39. >
  40. >opens an (existing) file starting with reading with the possibility to
  41. >switch to writing without closing.
  42. >
  43. >        fopen( "Any.dat", "a+" );
  44. >
  45. >opens for appending and maybe reading afterwards.
  46. >
  47. >The only thing to assure for read/write switches is to use an
  48. >fseek() call between read and write operations:
  49. >
  50. >        fp = fopen( "My.dat", "r+" );
  51. >        fwrite( "Hello", 1, 5, fp );
  52. >        fseek( fp, 0L, SEEK_SET );
  53.  
  54. You could have fflush(), rewind() or fsetpos() instead here.
  55.  
  56. >        fread( Message, 1, 5, fp );
  57. >        fseek( fp, 0L, SEEK_CUR );
  58.  
  59. You could have rewind() or fsetpos() instead here.
  60.  
  61. >        fwrite( " and Goodby.", 1, 12, fp );
  62. >        fclose( fp );
  63.  
  64. >The main difference between stdio calls and the "low-level" routines
  65. >(not only under DOS but for Unix too) is the absence of buffering in
  66. >the low-level functions.
  67.  
  68. The 'low-level' functions typically have buffering in the OS. However
  69. stdio typically implements an extra level of buffering local to the
  70. particular program.
  71.  
  72. >if you use fwrite(), for example, you can not be sure that the number of 
  73. >bytes you want to write are actually written to the output at that time.
  74. >The "real" write operation will take place if the stdio buffer for that
  75. >file is full.
  76.  
  77. A good implementation of fwrite could bypass the stdio buffering where the
  78. block sizes were appropriate.
  79.  
  80. -- 
  81. -----------------------------------------
  82. Lawrence Kirby | fred@genesis.demon.co.uk
  83. Wilts, England | 70734.126@compuserve.com
  84. -----------------------------------------
  85.